﻿<template>
  <a-drawer
    :destroyOnClose="true"
    :loading="loading"
    :title="title"
    :visible="visible"
    width="40%"
    @close=" () => { this.visible = false }"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="编号" prop="Code">
          <a-input v-model="entity.Code" autocomplete="off" placeholder="编号" />
        </a-form-model-item>
        <a-form-model-item label="名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" placeholder="名称" />
        </a-form-model-item>
        <a-form-model-item label="所属公司" prop="DeptId">
<!--          <a-input v-model="entity.DeptId" autocomplete="off" placeholder="所属公司" />-->
          <a-tree-select
            v-model="entity.DeptId"
            allowClear
            :treeData="ParentIdTreeData"
            placeholder="请选择组织"
            treeDefaultExpandAll
          ></a-tree-select>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remark">
          <a-input v-model="entity.Remark" autocomplete="off" placeholder="备注" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div :style="{ position: 'absolute', right: 0, bottom: 0, width: '100%', borderTop: '1px solid #e9e9e9', padding: '4px 16px', background: '#fff', textAlign: 'right', zIndex: 1 }">
      <a-button @click="close"> 取消</a-button>&nbsp;
      <a-button :loading="loading" type="primary" @click="handleSubmit"> 保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      ParentIdTreeData: []
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.$http.post('/Base_Manage/Base_Department/GetDepartmentTreeDataList', {}).then(resJson => {
        if (resJson.Success) {
          this.ParentIdTreeData = resJson.Data
        }
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/CostManagement/cost_center/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    close () {
      this.visible = false
      this.entity = {}
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/CostManagement/cost_center/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
